package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.itheima.constant.JwtClaimsConstant;
import com.itheima.dto.OrdersStatisticsDto;
import com.itheima.dto.UserLoginDTO;
import com.itheima.exception.BaseException;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.User;
import com.itheima.properties.JwtProperties;
import com.itheima.properties.WeChatProperties;
import com.itheima.service.UserService;
import com.itheima.vo.UserLoginVO;
import com.itheima.utils.HttpClientUtil;
import com.itheima.utils.JwtUtil;
import com.itheima.vo.UserReportVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    //微信服务接口地址
    public static final String WX_LOGIN="https://api.weixin.qq.com/sns/jscode2session";
    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private WeChatProperties weChatProperties;

    @Override
    public UserLoginVO wxLogin(UserLoginDTO userLoginDTO) {

        String openid = getOpenid(userLoginDTO.getCode());
        if (openid==null){
            throw new BaseException("服务器异常");
        }
        User user= userMapper.selectUserByCode(openid);
        if (user==null){
            user = new User();
            user.setOpenid(openid);
            user.setCreateTime(LocalDateTime.now());
            userMapper.insertUser(user);

        }

        Map<String, Object> map = new HashMap<>();
        map.put(JwtClaimsConstant.USER_ID,user.getId());
        map.put(JwtClaimsConstant.USERNAME,user.getPhone());
        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), map);
        return new UserLoginVO(user.getId(),user.getOpenid(),token);

    }

    @Override
    public UserReportVO userStatistics(OrdersStatisticsDto statisticsDto) {
        return userMapper.userStatistics(statisticsDto);
    }

    /**
     * 调用微信接口服务，获取微信用户的openid
     *
     * @param code
     * @return
     */
    private String getOpenid(String code) {
        //调用微信接口服务获取当前用户的openid
        Map<String, String> map = new HashMap<>();
        map.put("appid", weChatProperties.getAppid());
        map.put("secret", weChatProperties.getSecret());
        map.put("js_code", code);
        map.put("grant_type", "authorization_code");
        String json = HttpClientUtil.doGet(WX_LOGIN, map);

        JSONObject jsonObject = JSON.parseObject(json);
        String openid = jsonObject.getString("openid");

        return openid;
    }

    @Override
    public Integer getNewUser(LocalDateTime begin, LocalDateTime end) {
        return userMapper.getNewUser(begin,end);
    }

    @Override
    public Integer selectCount() {

        return userMapper.select();
    }
}
